/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.google.common.collect.Maps;
/*     */ import com.sun.mail.smtp.SMTPMessage;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.CacheEntryBy;
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.CacheEntry;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import com.zimbra.cs.httpclient.URLUtil;
/*     */ import com.zimbra.soap.account.type.DistributionListSubscribeOp;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.io.OutputStreamWriter;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import javax.activation.DataSource;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.Transport;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class DistributionListDocumentHandler
/*     */   extends AccountDocumentHandler
/*     */ {
/*     */   protected Element proxyIfNecessary(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*     */     try
/*     */     {
/*  68 */       Group group = getGroupBasic(request, Provisioning.getInstance());
/*     */       
/*  70 */       if (!Provisioning.onLocalServer(group)) {
/*  71 */         Server server = group.getServer();
/*  72 */         if (server == null) {
/*  73 */           throw ServiceException.PROXY_ERROR(AccountServiceException.NO_SUCH_SERVER(group.getAttr("zimbraMailHost")), "");
/*     */         }
/*     */         
/*     */ 
/*  77 */         return proxyRequest(request, context, server);
/*     */       }
/*     */       
/*  80 */       return null;
/*     */     }
/*     */     catch (ServiceException e)
/*     */     {
/*  84 */       throw e;
/*     */     }
/*     */   }
/*     */   
/*     */   protected Group getGroupBasic(Element request, Provisioning prov) throws ServiceException
/*     */   {
/*  90 */     Element eDL = request.getElement("dl");
/*  91 */     String key = eDL.getAttribute("by");
/*  92 */     String value = eDL.getText();
/*     */     
/*  94 */     Group group = prov.getGroupBasic(Key.DistributionListBy.fromString(key), value);
/*     */     
/*  96 */     if (group == null) {
/*  97 */       throw AccountServiceException.NO_SUCH_DISTRIBUTION_LIST(value);
/*     */     }
/*     */     
/* 100 */     return group;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected static void addGroupMembers(Provisioning prov, Group group, String[] members)
/*     */     throws ServiceException
/*     */   {
/* 117 */     group = loadFullGroupFromMaster(prov, group);
/* 118 */     prov.addGroupMembers(group, members);
/*     */     
/*     */ 
/* 121 */     flushAccountCache(prov, members);
/*     */   }
/*     */   
/*     */   protected static void removeGroupMembers(Provisioning prov, Group group, String[] members) throws ServiceException
/*     */   {
/* 126 */     group = loadFullGroupFromMaster(prov, group);
/* 127 */     prov.removeGroupMembers(group, members);
/*     */     
/*     */ 
/* 130 */     flushAccountCache(prov, members);
/*     */   }
/*     */   
/*     */   private static Group loadFullGroupFromMaster(Provisioning prov, Group group) throws ServiceException
/*     */   {
/* 135 */     if (!group.isDynamic()) {
/* 136 */       String groupName = group.getName();
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 145 */       group = prov.getGroup(Key.DistributionListBy.id, group.getId(), true);
/*     */       
/* 147 */       if (group == null) {
/* 148 */         throw AccountServiceException.NO_SUCH_DISTRIBUTION_LIST(groupName);
/*     */       }
/*     */     }
/*     */     
/* 152 */     return group;
/*     */   }
/*     */   
/*     */   private static void flushAccountCache(Provisioning prov, String[] members)
/*     */   {
/* 157 */     Map<String, List<Provisioning.CacheEntry>> remoteAccts = Maps.newHashMap();
/*     */     
/* 159 */     for (String member : members) {
/*     */       try {
/* 161 */         Account acct = prov.get(Key.AccountBy.name, member);
/* 162 */         if ((acct != null) && 
/* 163 */           (!Provisioning.onLocalServer(acct)))
/*     */         {
/*     */ 
/* 166 */           Server server = acct.getServer();
/* 167 */           String serverName = server.getName();
/* 168 */           List<Provisioning.CacheEntry> acctsOnServer = (List)remoteAccts.get(serverName);
/* 169 */           if (acctsOnServer == null) {
/* 170 */             acctsOnServer = Lists.newArrayList();
/* 171 */             remoteAccts.put(serverName, acctsOnServer);
/*     */           }
/* 173 */           acctsOnServer.add(new Provisioning.CacheEntry(Key.CacheEntryBy.id, acct.getId()));
/*     */         }
/*     */         
/*     */ 
/*     */       }
/*     */       catch (ServiceException e)
/*     */       {
/*     */ 
/* 181 */         ZimbraLog.account.warn("unable to flush account cache", e);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 202 */     SoapProvisioning soapProv = new SoapProvisioning();
/* 203 */     String adminUrl = null;
/* 204 */     for (Map.Entry<String, List<Provisioning.CacheEntry>> acctsOnServer : remoteAccts.entrySet()) {
/* 205 */       String serverName = (String)acctsOnServer.getKey();
/* 206 */       List<Provisioning.CacheEntry> accts = (List)acctsOnServer.getValue();
/*     */       try
/*     */       {
/* 209 */         Server server = prov.get(Key.ServerBy.name, serverName);
/* 210 */         adminUrl = URLUtil.getAdminURL(server, "/service/admin/soap/", true);
/* 211 */         soapProv.soapSetURI(adminUrl);
/* 212 */         soapProv.soapZimbraAdminAuthenticate();
/* 213 */         soapProv.flushCache(CacheEntryType.account, (Provisioning.CacheEntry[])accts.toArray(new Provisioning.CacheEntry[accts.size()]));
/*     */       }
/*     */       catch (ServiceException e) {
/* 216 */         ZimbraLog.account.warn("unable to flush account cache on remote server: " + serverName, e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected static abstract class SynchronizedGroupHandler {
/*     */     protected Group group;
/*     */     
/*     */     protected SynchronizedGroupHandler(Group group) {
/* 225 */       this.group = group;
/*     */     }
/*     */     
/*     */     protected abstract void handleRequest() throws ServiceException;
/*     */     
/*     */     protected void handle() throws ServiceException {
/* 231 */       synchronized (this.group) {
/* 232 */         handleRequest();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected static abstract class NotificationSender
/*     */   {
/*     */     protected Provisioning prov;
/*     */     protected Group group;
/*     */     protected Account requestingAcct;
/*     */     protected DistributionListSubscribeOp op;
/*     */     
/*     */     protected NotificationSender(Provisioning prov, Group group, Account requestingAcct, DistributionListSubscribeOp op) {
/* 245 */       this.prov = prov;
/* 246 */       this.group = group;
/* 247 */       this.requestingAcct = requestingAcct;
/* 248 */       this.op = op;
/*     */     }
/*     */     
/*     */     protected abstract MimeMultipart buildMailContent(Locale paramLocale) throws MessagingException;
/*     */     
/*     */     protected Locale getLocale(Account acct) throws ServiceException
/*     */     {
/* 255 */       return acct.getLocale();
/*     */     }
/*     */     
/*     */     protected void buildContentAndSend(SMTPMessage out, Locale locale, String logTTxt)
/*     */       throws MessagingException
/*     */     {
/* 261 */       MimeMultipart mmp = buildMailContent(locale);
/* 262 */       out.setContent(mmp);
/* 263 */       Transport.send(out);
/*     */       
/*     */ 
/* 266 */       Address[] rcpts = out.getRecipients(Message.RecipientType.TO);
/* 267 */       StringBuilder rcptAddr = new StringBuilder();
/* 268 */       for (Address a : rcpts) {
/* 269 */         rcptAddr.append(a.toString() + ", ");
/*     */       }
/* 271 */       ZimbraLog.account.info(logTTxt + ": rcpt='" + rcptAddr + "' Message-ID=" + out.getMessageID());
/*     */     }
/*     */     
/*     */     protected static abstract class MimePartDataSource
/*     */       implements DataSource
/*     */     {
/*     */       private String mText;
/* 278 */       private byte[] mBuf = null;
/*     */       
/*     */       public MimePartDataSource(String text) {
/* 281 */         this.mText = text;
/*     */       }
/*     */       
/*     */       public InputStream getInputStream() throws IOException
/*     */       {
/* 286 */         synchronized (this) {
/* 287 */           if (this.mBuf == null) {
/* 288 */             ByteArrayOutputStream buf = new ByteArrayOutputStream();
/* 289 */             OutputStreamWriter wout = new OutputStreamWriter(buf, "utf-8");
/*     */             
/* 291 */             String text = this.mText;
/* 292 */             wout.write(text);
/* 293 */             wout.flush();
/* 294 */             this.mBuf = buf.toByteArray();
/*     */           }
/*     */         }
/* 297 */         return new ByteArrayInputStream(this.mBuf);
/*     */       }
/*     */       
/*     */       public OutputStream getOutputStream()
/*     */       {
/* 302 */         throw new UnsupportedOperationException();
/*     */       }
/*     */     }
/*     */     
/*     */     protected static class HtmlPartDataSource extends DistributionListDocumentHandler.NotificationSender.MimePartDataSource
/*     */     {
/*     */       private static final String CONTENT_TYPE = "text/html; charset=utf-8";
/*     */       private static final String NAME = "HtmlDataSource";
/*     */       
/*     */       HtmlPartDataSource(String text)
/*     */       {
/* 313 */         super();
/*     */       }
/*     */       
/*     */       public String getContentType()
/*     */       {
/* 318 */         return "text/html; charset=utf-8";
/*     */       }
/*     */       
/*     */       public String getName()
/*     */       {
/* 323 */         return "HtmlDataSource";
/*     */       }
/*     */     }
/*     */     
/*     */     protected static class XmlPartDataSource extends DistributionListDocumentHandler.NotificationSender.MimePartDataSource
/*     */     {
/*     */       private static final String CONTENT_TYPE = "xml/x-zimbra-dl-subscription; charset=utf-8";
/*     */       private static final String NAME = "XmlDataSource";
/*     */       
/*     */       XmlPartDataSource(String text)
/*     */       {
/* 334 */         super();
/*     */       }
/*     */       
/*     */       public String getContentType()
/*     */       {
/* 339 */         return "xml/x-zimbra-dl-subscription; charset=utf-8";
/*     */       }
/*     */       
/*     */       public String getName()
/*     */       {
/* 344 */         return "XmlDataSource";
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/DistributionListDocumentHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */